import json
import re
import time
from json import JSONDecodeError
import pymssql
import requests
from lxml import etree


class getIP(object):
    def __init__(self):
        self.proxy_url = 'https://raw.githubusercontent.com/fate0/proxylist/master/proxy.list'
        self.test_url = 'https://www.prettylittlething.com/'
        self.db = pymssql.connect(host='127.0.0.1', user='sa', password='654321', port='1433',
                                  database='ip', charset="utf8")
        self.cursor = self.db.cursor()

    def save_data(self, host, port, type, test_address, test_time):
        try:
            sql = "insert into iphub(host,port,type,test_address,test_time) values(%s,%s,%s,%s,%s)"
            self.cursor.execute(sql, (host, port, type, test_address, test_time))
            self.db.commit()
            print(host, port, type, test_address, test_time)
        except Exception as e:
            print(e)
            self.db.rollback()

    # 检测访问时间
    def testIP(self, ip, port, type):
        flag = False
        proxie = {
            type: '{}:{}'.format(ip, port)
        }
        try:
            r = requests.get(self.test_url, proxies=proxie, timeout=3)
            if r.status_code == 200:
                flag = True
                self.time = r.elapsed.total_seconds()
        except Exception as e:
            print("error")
        finally:
            return flag


    # 获取代理服务器ip
    def get_url_id(self):
        response = requests.get(self.proxy_url)
        proxies_list = response.text.split('\n')
        for proxy_str in proxies_list:
            proxy_json = json.loads(proxy_str)
            host = proxy_json['host']
            port = proxy_json['port']
            type = proxy_json['type']
            # self.verify(host, port, type)
            flag = self.testIP(host, port, type)
            if flag:
                self.save_data(host, port, type, self.test_url, self.time)

    # 获取ip地址所属地
    def getIpArea(self, ip, port, type):
        hreader = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.87 Safari/537.36',
            'Origin': 'http://ip.tool.chinaz.com',
            'Referer': 'http://ip.tool.chinaz.com/',
            'Host': 'ip.tool.chinaz.com'
        }
        proxie = {
            type: '{}:{}'.format(ip, port)
        }
        url = 'http://ip.tool.chinaz.com/{}'.format(ip)
        html = requests.get(url, headers=hreader, proxies=proxie)
        # print(html.text)
        html = etree.HTML(html.text)
        address = html.xpath('//*[@id="leftinfo"]/div[3]/div[2]/p[2]/span[4]/text()')
        return address[0]

    def Main(self):
        self.get_url_id()


if __name__ == '__main__':
    gip = getIP()
    gip.Main()

